package com.example.gallery;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.example.board.R;


public class AsyncImageList extends Activity implements OnItemClickListener{
	Common common;
	SharedPreferences pref;
	
	ArrayList<AlbumDTO> list = new ArrayList<AlbumDTO>();
	final String TAG = "ImageList";
	String folder = "";	
	String[] image_list;
	
	GridView grid_image;
	Activity acti = this;
	ListView listView ;
	TextView tv_folder; 
	ImageListAdapter adapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_list);
		
		DisplayMetrics displayMetrics = new DisplayMetrics();

//		Display dis = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
		
		tv_folder = (TextView) findViewById(R.id.tv_folder);
		grid_image = (GridView) findViewById(R.id.grid_image);
		grid_image.setOnItemClickListener(this);
		
		listView = (ListView) findViewById(android.R.id.list);
		
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		
		int deviceWidth = displayMetrics.widthPixels;
		int deviceHeight = displayMetrics.heightPixels;

		Log.i(TAG, "device_width : " + deviceWidth);
		Log.i(TAG, "device_height : " + deviceHeight);
		
		Intent intent = getIntent();
		folder = intent.getStringExtra("folder");
		Log.i(TAG, "folder : " + folder);
		
		/*pref = getSharedPreferences("folder",0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putString("folder", folder);
		editor.commit();*/
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.i(TAG, "onResume");
		common = new Common();
		
		tv_folder.setText(folder);
		list = common.getImageFile(folder);	//해당 폴더에 저장된 사진 리스트
		Log.i(TAG, "사진 수 : "+list.size());
		
		for(int i=0; i<list.size(); i++){
			Log.i(TAG, i +". 사진 :"+list.get(i).getA_image());	
		}
		
//		adapter = new ImageListAdapter(this, list);
		grid_image.setAdapter(adapter);
	
		
	}
	public void updateUI(){
//		adapter = new ImageListAdapter(this, list);
		grid_image.setAdapter(adapter);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
			Log.i(TAG, "position : "+position);
		
			Intent intent = new Intent(this, FullImageActivity.class);
			intent.putExtra("position", position);
			startActivity(intent);
	}
	
	
/*	public String[] getImageList(ArrayList<AlbumDTO> list){
		
		for(int i = 0; i<list.size(); i++){
			image_list[i] = list.get(i).a_image;
		}
		return image_list;
	}*/
	
	

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
	
	 /*class ImageListAdapter extends BaseAdapter{
			LayoutInflater inflater;
			ArrayList<AlbumDTO> list;
			Context mcontext ;	//상위 클래스로부터 context를 받음 -> imageview를 상위로 보내야 하므로
			String[] image_list;
			
			public ImageListAdapter(Context context) {
				super();
				mcontext = context;
//				inflater = (LayoutInflater) acti.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			}
			
			
			
			public ImageListAdapter(ImageList imageList,
					ArrayList<AlbumDTO> list2) {
				// TODO Auto-generated constructor stub
			}



			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public Object getItem(int arg0) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public long getItemId(int arg0) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public View getView(int arg0, View arg1, ViewGroup arg2) {
				// TODO Auto-generated method stub
				return null;
			}
	 
	 }	//inner class
	 */
	
}
